from django.shortcuts import render
from django.http import HttpResponse
import os,time
from . models import *

#商品添加
def goodsadd(request):

    types = Types.objects.extra(select = {'_has':'concat(path,id)'}).order_by('_has')

    for v in types:
        n = v.path.count(',')-1
        v.name = '|----'*n+v.name

    # 分配数据
    context = {"types":types}
    
    return render(request,'myadmin/goods/add.html',context)

# 商品数据添加
def goodsinsert(request):

    # 判断是否有商品图片上传
    myfile = request.FILES.get("pic",None)
    if not myfile:
        return HttpResponse('<script>alert("必须上传商品图片");location.href="/admin/goods/add";</script>')
        
    
    # 接受数据,添加
    ob = Goods()
    ob.typeid = request.POST['pid']
    ob.title = request.POST['title']
    ob.company = request.POST['company']
    ob.descr = request.POST['descr']
    ob.price = request.POST['price']
    ob.store = request.POST['store']
    ob.clicknum = 0
    ob.num = 0
    ob.picname = myadmin_upload(request)
    ob.state = 1
    ob.save()


    return HttpResponse('<script>alert("商品上传成功");location.href="/admin/goods/index";</script>')

# 执行文件上传操作
def myadmin_upload(request): 
    #执行图片的上传
    myfile = request.FILES.get("pic",None)
    filename = str(time.time())+"."+myfile.name.split('.').pop()
    destination = open("./static/goods/"+filename,"wb+")
    for chunk in myfile.chunks():      # 分块写入文件  
        destination.write(chunk)  
    destination.close()
    return filename

# 商品列表
def goodsindex(request):
    # 获取所有商品数据
    ob = Goods.objects.all()

    for x in ob:
        t = Types.objects.get(id=x.typeid)
        x.pname = t.name
        

    # 分配数据
    context = {'glist':ob}

    # 解析模板
    return render(request,'myadmin/goods/index.html',context)


# 商品删除
def goodsdel(request,gid):

    try:
        # 获取商品对象
        ob = Goods.objects.get(id=gid)
        # 执行删除
        os.remove('./static/goods/'+ob.picname)
        ob.delete()
        return HttpResponse('<script>alert("商品删除成功");location.href="/admin/goods/index";</script>')
    except:
        return HttpResponse('<script>alert("商品删除失败");location.href="/admin/goods/index";</script>')


# 商品修改
def goodsedit(request,gid):
    
    # 根据商品id.,获取商品信息
    ob = Goods.objects.get(id=gid)
    T = Types.objects.get(id=ob.typeid)
    ob.pname = T.name

    # 分配数据
    context = {'ginfo':ob}

    # 解析模板
    return render(request,'myadmin/goods/edit.html',context)


# 执行商品修改
def goodsupdate(request):
    try:
        # 根据gid获取当前商品对象
        ob = Goods.objects.get(id=request.POST['gid'])

        ob.title = request.POST['title']
        ob.price = request.POST['price']
        ob.store = request.POST['store']
        ob.company = request.POST['company']
        ob.descr = request.POST['descr']
        ob.state = request.POST['state']

        # 判断是否有图片上传
        myfile = request.FILES.get("pic",None)
        if myfile:
            os.remove('./static/goods/'+ob.picname)
            ob.picname = myadmin_upload(request);

        ob.save()
        return HttpResponse('<script>alert("商品修改成功");location.href="/admin/goods/index";</script>')
    except:
        return HttpResponse('<script>alert("商品修改失败");location.href="/admin/goods/edit/'+ob.id+'";</script>')







    